package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.tb.lmt.domain.vo.TbEquipmentApplyVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbRepairplanlistApprovedBo;
import com.ruoyi.tb.lmt.domain.vo.TbRepairplanlistApprovedVo;
import com.ruoyi.tb.lmt.domain.TbRepairplanlistApproved;
import com.ruoyi.tb.lmt.mapper.TbRepairplanlistApprovedMapper;
import com.ruoyi.tb.lmt.service.ITbRepairplanlistApprovedService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * //大机报告申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-25
 */
@RequiredArgsConstructor
@Service
public class TbRepairplanlistApprovedServiceImpl implements ITbRepairplanlistApprovedService {

    private final TbRepairplanlistApprovedMapper baseMapper;

    @Autowired
    private ISysRoleService iSysRoleService;

    @Autowired
    private TbRepairplanlistApprovedMapper tbRepairplanlistApprovedMapper;

    /**
     * 查询//大机报告申请
     */
    @Override
    public TbRepairplanlistApprovedVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询//大机报告申请列表
     */
    @Override
    public TableDataInfo<TbRepairplanlistApprovedVo> queryPageList(TbRepairplanlistApprovedBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbRepairplanlistApproved> lqw = buildQueryWrapper(bo);

        // 在这里添加根据 planDate 降序排序
        pageQuery.setOrderByColumn("approvedStatus");
        pageQuery.setIsAsc("asc");

        Page<TbRepairplanlistApprovedVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询//大机报告申请列表
     */
    @Override
    public List<TbRepairplanlistApprovedVo> queryList(TbRepairplanlistApprovedBo bo) {
        LambdaQueryWrapper<TbRepairplanlistApproved> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbRepairplanlistApproved> buildQueryWrapper(TbRepairplanlistApprovedBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbRepairplanlistApproved> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanId() != null, TbRepairplanlistApproved::getPlanId, bo.getPlanId());
        lqw.eq(bo.getApprovedId() != null, TbRepairplanlistApproved::getApprovedId, bo.getApprovedId());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtType()), TbRepairplanlistApproved::getLmtType, bo.getLmtType());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TbRepairplanlistApproved::getLmtNumber, bo.getLmtNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairType()), TbRepairplanlistApproved::getRepairType, bo.getRepairType());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairTimes()), TbRepairplanlistApproved::getRepairTimes, bo.getRepairTimes());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairCode()), TbRepairplanlistApproved::getRepairCode, bo.getRepairCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairChar()), TbRepairplanlistApproved::getRepairChar, bo.getRepairChar());
        lqw.eq(bo.getRepairStartDate() != null, TbRepairplanlistApproved::getRepairStartDate, bo.getRepairStartDate());
        lqw.eq(bo.getRepairEndDate() != null, TbRepairplanlistApproved::getRepairEndDate, bo.getRepairEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairDays()), TbRepairplanlistApproved::getRepairDays, bo.getRepairDays());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairUnit()), TbRepairplanlistApproved::getRepairUnit, bo.getRepairUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbRepairplanlistApproved::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), TbRepairplanlistApproved::getRemark, bo.getRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkerTeam()), TbRepairplanlistApproved::getWorkerTeam, bo.getWorkerTeam());
        lqw.eq(bo.getCreateTime() != null, TbRepairplanlistApproved::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbRepairplanlistApproved::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbRepairplanlistApproved::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbRepairplanlistApproved::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckUser2()), TbRepairplanlistApproved::getCheckUser2, bo.getCheckUser2());
        lqw.eq(bo.getCheckTime2() != null, TbRepairplanlistApproved::getCheckTime2, bo.getCheckTime2());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckUser3()), TbRepairplanlistApproved::getCheckUser3, bo.getCheckUser3());
        lqw.eq(bo.getCheckTime3() != null, TbRepairplanlistApproved::getCheckTime3, bo.getCheckTime3());
        lqw.eq(bo.getStartTime() != null, TbRepairplanlistApproved::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, TbRepairplanlistApproved::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbRepairplanlistApproved::getWorkGroup, bo.getWorkGroup());
        lqw.eq(bo.getWorkGroupId() != null, TbRepairplanlistApproved::getWorkGroupId, bo.getWorkGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getApplicant()), TbRepairplanlistApproved::getApplicant, bo.getApplicant());
        lqw.eq(bo.getApprovedTime() != null, TbRepairplanlistApproved::getApprovedTime, bo.getApprovedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovedUser()), TbRepairplanlistApproved::getApprovedUser, bo.getApprovedUser());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovedStatus()), TbRepairplanlistApproved::getApprovedStatus, bo.getApprovedStatus());
        return lqw;
    }

    /**
     * 新增//大机报告申请
     */
    @Override
    public Boolean insertByBo(TbRepairplanlistApprovedBo bo) {
        TbRepairplanlistApproved add = BeanUtil.toBean(bo, TbRepairplanlistApproved.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改//大机报告申请
     */
    @Override
    public Boolean updateByBo(TbRepairplanlistApprovedBo bo) {
        TbRepairplanlistApproved update = BeanUtil.toBean(bo, TbRepairplanlistApproved.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbRepairplanlistApproved entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除//大机报告申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TbRepairplanlistApprovedVo> selectApplyList(PageQuery pageQuery) {

        //获取当前用户内容
        LoginUser loginUser = LoginHelper.getLoginUser();
        String username = loginUser.getUsername();
        Long deptId = loginUser.getDeptId();

        // 检查当前用户的角色
        boolean isAdmin = isAdmin(username); // 是否为管理员
        boolean isEquipmentDept = isEquipmentDept(username); // 是否为设备科人员
        boolean isAcceptanceRoom = isAcceptanceRoom(username); // 是否为车间副主任

        // 查询符合条件的大部件申请列表
        List<TbRepairplanlistApprovedVo> result = null;
        if (isAdmin) {
            // 管理员可以看到所有申请列表
            result = tbRepairplanlistApprovedMapper.selectApplyList();
        } else if (isEquipmentDept) {
            // 设备科人员可以看到所有申请列表
            result = tbRepairplanlistApprovedMapper.selectApplyList();
        } else if (isAcceptanceRoom) {
            // 验收室可以看到所有申请列表
            result = tbRepairplanlistApprovedMapper.selectApplyList();
        } else {
            // 其他用户只能看到自己提交的申请列表
            result = tbRepairplanlistApprovedMapper.selectApplyListByUsername(username);
        }
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TbRepairplanlistApprovedVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    public boolean isAdmin(String username) {
        // 通过用户名获取用户的所有角色
        List<SysRole> userRoles = iSysRoleService.selectRolesByUserName(username);

        // 检查用户是否有管理员的角色标识
        for (SysRole role : userRoles) {
            if ("admin".equals(role.getRoleKey())) {
                return true;
            }
        }
        return false;
    }
    public boolean isEquipmentDept(String username) {
        // 通过用户名获取用户的所有角色
        List<SysRole> userRoles = iSysRoleService.selectRolesByUserName(username);

        // 检查用户是否有设备科的角色标识
        for (SysRole role : userRoles) {
            if ("equipmentDept".equals(role.getRoleKey())) {
                return true;
            }
        }
        return false;
    }
    public boolean isAcceptanceRoom(String username) {
        // 通过用户名获取用户的所有角色
        List<SysRole> userRoles = iSysRoleService.selectRolesByUserName(username);

        // 检查用户是否有验收室的角色标识
        for (SysRole role : userRoles) {
            if ("acceptanceRoom".equals(role.getRoleKey())) {
                return true;
            }
        }
        return false;
    }
}
